package com.aier.bean.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @类名:CommonResult
 * @描述:统一返回类型
 * @作者:buckwheat
 * @时间:2022/4/26-20:30
 * @版本:
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    private String code;
    private String message;
    private T data;
    public CommonResult(String code, String message){
        this(code,message,null);
    }

    public static CommonResult error(GlobalCode globalCode){
        CommonResult result = new CommonResult();
        result.setCode(globalCode.getCode());
        result.setMessage(globalCode.getMessage());
        return result;
    }

    public static CommonResult error(String code, String message){
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static CommonResult error(String code, Object data, String message){
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static CommonResult success(Object data){
        CommonResult result = new CommonResult();
        result.setCode(GlobalCode.REQUEST_SUCCESS.getCode());
        result.setMessage(GlobalCode.REQUEST_SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static CommonResult success(){
        CommonResult result = new CommonResult();
        result.setCode(GlobalCode.REQUEST_SUCCESS.getCode());
        result.setMessage(GlobalCode.REQUEST_SUCCESS.getMessage());
        return result;
    }


}
